package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.bo.PostageTemplateBO;
import com.junzhiit.nengji.cms.service.PostageTemplateService;
import com.junzhiit.nengji.cms.web.exception.PostageTemplateNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.vo.PostageTemplateAddVO;
import com.junzhiit.nengji.cms.web.vo.PostageTemplateDetailVO;
import com.junzhiit.nengji.cms.web.vo.PostageTemplateEditVO;
import com.junzhiit.nengji.cms.web.vo.PostageTemplateVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.PostageConditionDAO;
import com.junzhiit.nengji.dao.PostageTemplateDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.PostageCondition;
import com.junzhiit.nengji.model.PostageTemplate;
import com.junzhiit.nengji.model.Shop;

@Service
public class PostageTemplateServiceImpl extends BaseServiceImpl implements PostageTemplateService {
	
	@Autowired
	private PostageTemplateDAO postageTemplateDAO;
	
	@Autowired
	private PostageConditionDAO postageConditionDAO;
	
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public ResultVO add(PostageTemplateAddVO postageTemplateAddVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(postageTemplateAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PostageTemplate postageTemplate=new PostageTemplate();
		BeanUtils.copyProperties(postageTemplateAddVO, postageTemplate);
		postageTemplate.setShopId(shop.getId());
		
		PostageTemplate data=postageTemplateDAO.insert(postageTemplate);
		
		List<PostageCondition> postageConditions=JsonUtil.toListObject(postageTemplateAddVO.getPostageConditionAddVOs(), PostageCondition.class);
		
		for(PostageCondition postageCondition:postageConditions){
			postageCondition.setPostageTemplateId(data.getId());
			postageCondition.setCreatedTime(new Date());
		}
		
		postageConditionDAO.batchInsert(postageConditions);
		
		
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO edit(PostageTemplateEditVO postageTemplateEditVO){
		ResultVO result=new ResultVO();
		PostageTemplate postageTemplate=postageTemplateDAO.query(postageTemplateEditVO.getId());
		if(postageTemplate==null){
			throw new PostageTemplateNotFoundException("运费模板不存在");
		}
		
		BeanUtils.copyProperties(postageTemplateEditVO, postageTemplate);
		postageTemplateDAO.update(postageTemplate);
		
		List<PostageCondition> postageConditions=JsonUtil.toListObject(postageTemplateEditVO.getPostageConditionAddVOs(), PostageCondition.class);
		
		for(PostageCondition postageCondition:postageConditions){
			postageCondition.setPostageTemplateId(postageTemplate.getId());
			postageCondition.setCreatedTime(new Date());
		}
		
		postageConditionDAO.deleteByPostageTemplateId(postageTemplate.getId());
		postageConditionDAO.batchInsert(postageConditions);
		
		return result;
	}
	
	@Override
	public ResultVO getAllBO(PostageTemplateVO postageTemplateVO){
		
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(postageTemplateVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		
		PostageTemplate postageTemplate=new PostageTemplate();
		postageTemplate.setShopId(shop.getId());
		
		List<PostageTemplateBO> postageTemplateBOs=postageTemplateDAO.getAllBO(postageTemplate);
		
		Map<String, PostageTemplateDetailVO> map = new LinkedHashMap<String, PostageTemplateDetailVO>();
		
		for (PostageTemplateBO bo : postageTemplateBOs) {
			PostageTemplateDetailVO rbo = map.get(bo.getId());
	        if (rbo==null) {
	        	rbo = new PostageTemplateDetailVO();
	        	rbo.setName(bo.getName());
	        	rbo.setValuateType(bo.getValuateType());
	        	rbo.setId(bo.getId());
	        	rbo.setUseCount(bo.getUseCount());
	            List<PostageCondition> childrens = new ArrayList<PostageCondition>();
	            rbo.setChildrens(childrens);
	            map.put(bo.getId(),rbo);
	        }
	        
	        PostageCondition postageCondition=new PostageCondition();
	        BeanUtils.copyProperties(bo, postageCondition);
	        rbo.getChildrens().add(postageCondition);
	    }
		Collection<PostageTemplateDetailVO> data = map.values();
		
		result.setData(data);
		
		return result;
	}
	
	
	@Override
	public ResultVO getBO(PostageTemplateVO postageTemplateVO){
		
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(postageTemplateVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		PostageTemplate postageTemplate=new PostageTemplate();
		postageTemplate.setShopId(shop.getId());
		postageTemplate.setId(postageTemplateVO.getId());
		
		List<PostageTemplateBO> postageTemplateBOs=postageTemplateDAO.getAllBO(postageTemplate);
		
		Map<String, PostageTemplateDetailVO> map = new LinkedHashMap<String, PostageTemplateDetailVO>();
		
		for (PostageTemplateBO bo : postageTemplateBOs) {
			PostageTemplateDetailVO rbo = map.get(bo.getId());
	        if (rbo==null) {
	        	rbo = new PostageTemplateDetailVO();
	        	rbo.setName(bo.getName());
	        	rbo.setValuateType(bo.getValuateType());
	        	rbo.setId(bo.getId());
	            List<PostageCondition> childrens = new ArrayList<PostageCondition>();
	            rbo.setChildrens(childrens);
	            map.put(bo.getId(),rbo);
	        }
	        
	        PostageCondition postageCondition=new PostageCondition();
	        BeanUtils.copyProperties(bo, postageCondition);
	        rbo.getChildrens().add(postageCondition);
	    }
		
		PostageTemplateDetailVO data=map.get(postageTemplateVO.getId());
		result.setData(data);
		
		return result;
	}

	@Override
	public ResultVO getAll(String sessionId){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		List<PostageTemplate> data=postageTemplateDAO.getAll(shop.getId());
		result.setData(data);
		
		return result;
		
	}
	
	@Override
	public ResultVO delete(String id){
		ResultVO result=new ResultVO();
		postageTemplateDAO.updateToDelete(id);
		postageConditionDAO.updateToDeleteByPostageTemplateId(id);
		return result;
	}
	
	

}
